package com.example;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.SimpleAccountRealm;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.junit.Before;
import org.junit.Test;

/**
 * @ClassName QuickStartTest4_2
 * @Description TODO
 * @Author zhaoluowei
 * @Date 2019/6/30  22:36
 * @Version 1.0
 * <p>
 * shiro 授权环节
 */

public class QuickStartTest5_2 {

    //测试授权
    @Test
    public void test() {
        //创建SecurityManager工厂
        Factory<SecurityManager> managerFactory = new IniSecurityManagerFactory("classpath:shiro.ini");

        SecurityManager factoryInstance = managerFactory.getInstance();

        //将SecurityManager 设置当前的运行环境中
        SecurityUtils.setSecurityManager(factoryInstance);

        Subject subject = SecurityUtils.getSubject();
        //构造需要登录用户信息 用户用的
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken("zhangsan", "123");

        subject.login(usernamePasswordToken);
        System.out.println(" 认证结果 -->  " + subject.isAuthenticated());
        System.out.println(" 是否有对应角色 -->  " + subject.hasRole("add"));
        System.out.println(" Principal  -->  " + subject.getPrincipal());
        subject.checkPermission("add");
        subject.checkPermission("update");
        subject.checkPermission("delete");
        System.out.println("是否有add权限 --> " + subject.isPermitted("add"));
        System.out.println("是否有update权限 --> " + subject.isPermitted("update"));
        System.out.println("是否有delete权限 --> " + subject.isPermitted("delete"));
        System.out.println("是否有insert权限 --> " + subject.isPermitted("insert"));
        subject.logout();
        System.out.println(" 认证结果 -->  " + subject.isAuthenticated());


    }


    //测试授权
    @Test
    public void test2() {
        //创建SecurityManager工厂
        Factory<SecurityManager> managerFactory = new IniSecurityManagerFactory("classpath:shiro.ini");

        SecurityManager factoryInstance = managerFactory.getInstance();

        //将SecurityManager 设置当前的运行环境中
        SecurityUtils.setSecurityManager(factoryInstance);

        Subject subject = SecurityUtils.getSubject();


        //构造需要登录用户信息 用户用的
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken("zlw", "123");

        subject.login(usernamePasswordToken);
        System.out.println(" 认证结果 -->  " + subject.isAuthenticated());
        System.out.println(" 是否有对应 admin 角色 -->  " + subject.hasRole("admin"));
        System.out.println(" 是否有对应 root 角色 -->  " + subject.hasRole("root"));
        System.out.println(" Principal  -->  " + subject.getPrincipal());
        subject.checkPermission("add");
        subject.checkPermission("update");
        subject.checkPermission("delete");
        System.out.println("是否有add权限 --> " + subject.isPermitted("add"));
        System.out.println("是否有update权限 --> " + subject.isPermitted("update"));
        System.out.println("是否有delete权限 --> " + subject.isPermitted("delete"));
        System.out.println("是否有insert权限 --> " + subject.isPermitted("insert"));
        subject.logout();
        System.out.println(" 认证结果 -->  " + subject.isAuthenticated());


    }


}
